package com.example.yuntest.controller;

import com.example.yuntest.job.MyJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RequestMapping("/test")
@RestController
public class TestController {


    @GetMapping("hello")
    public String hello() throws SchedulerException {

        // 1 调度器
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        // 2 任务调度
        String jobId = UUID.randomUUID().toString();
        JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
                .withIdentity(jobId, "hello")
                .build();
        // 3 触发器
        String triggerId = UUID.randomUUID().toString();
        SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(triggerId, "hello")
                .withSchedule(SimpleScheduleBuilder
                        .simpleSchedule()
                        .withIntervalInSeconds(2)
                )
                .build();

        scheduler.scheduleJob(jobDetail,trigger);


        return "<h1>111111111111111 hello world</h1>";
    }
}
